home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Source / Property Editors / stredit.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  9KB  |  335 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {       TStrings property editor dialog                 }
  6. {                                                       }
  7. {       Copyright (c) 1999 Borland International        }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit StrEdit;
  12.  
  13. interface
  14.  
  15. uses Windows, Classes, Graphics, Forms, Controls, Buttons, Dialogs, DsgnIntf,
  16.   StdCtrls, ExtCtrls, ComCtrls, Menus;
  17.  
  18. type
  19.   TStrEditDlg = class(TForm)
  20.     LineCount: TLabel;
  21.     CodeWndBtn: TButton;
  22.     OpenDialog: TOpenDialog;
  23.     SaveDialog: TSaveDialog;
  24.     HelpButton: TButton;
  25.     OKButton: TButton;
  26.     CancelButton: TButton;
  27.     Memo: TRichEdit;
  28.     StringEditorMenu: TPopupMenu;
  29.     LoadItem: TMenuItem;
  30.     SaveItem: TMenuItem;
  31.     CodeEditorItem: TMenuItem;
  32.     procedure FileOpen(Sender: TObject);
  33.     procedure FileSave(Sender: TObject);
  34.     procedure UpdateStatus(Sender: TObject);
  35.     procedure FormCreate(Sender: TObject);
  36.     procedure Memo1KeyDown(Sender: TObject; var Key: Word;
  37.       Shift: TShiftState);
  38.     procedure HelpButtonClick(Sender: TObject);
  39.     procedure CodeWndBtnClick(Sender: TObject);
  40.   private
  41.     SingleLine: string;
  42.     MultipleLines: string;
  43.     FModified: Boolean;
  44.   end;
  45.  
  46. type
  47.   TStringListProperty = class(TClassProperty)
  48.   public
  49.     function GetAttributes: TPropertyAttributes; override;
  50.     procedure Edit; override;
  51.   end;
  52.  
  53. implementation
  54.  
  55. {$R *.DFM}
  56.  
  57. uses ActiveX, SysUtils, DesignConst, LibHelp, ToolsAPI, IStreams, StFilSys,
  58.   TypInfo;
  59.  
  60. type
  61.   TStringsModuleCreator = class(TInterfacedObject, IOTACreator, IOTAModuleCreator)
  62.   private
  63.     FFileName: string;
  64.     FStream: TStringStream;
  65.     FAge: TDateTime;
  66.   public
  67.     constructor Create(const FileName: string; Stream: TStringStream; Age: TDateTime);
  68.     destructor Destroy; override;
  69.     { IOTACreator }
  70.     function GetCreatorType: string;
  71.     function GetExisting: Boolean;
  72.     function GetFileSystem: string;
  73.     function GetOwner: IOTAModule;
  74.     function GetUnnamed: Boolean;
  75.     { IOTAModuleCreator }
  76.     function GetAncestorName: string;
  77.     function GetImplFileName: string;
  78.     function GetIntfFileName: string;
  79.     function GetFormName: string;
  80.     function GetMainForm: Boolean;
  81.     function GetShowForm: Boolean;
  82.     function GetShowSource: Boolean;
  83.     function NewFormFile(const FormIdent, AncestorIdent: string): IOTAFile;
  84.     function NewImplSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
  85.     function NewIntfSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
  86.     procedure FormCreated(const FormEditor: IOTAFormEditor);
  87.   end;
  88.  
  89.   TOTAFile = class(TInterfacedObject, IOTAFile)
  90.   private
  91.     FSource: string;
  92.     FAge: TDateTime;
  93.   public
  94.     constructor Create(const ASource: string; AAge: TDateTime);
  95.     { IOTAFile }
  96.     function GetSource: string;
  97.     function GetAge: TDateTime;
  98.   end;
  99.  
  100. { TOTAFile }
  101.  
  102. constructor TOTAFile.Create(const ASource: string; AAge: TDateTime);
  103. begin
  104.   inherited Create;
  105.   FSource := ASource;
  106.   FAge := AAge;
  107. end;
  108.  
  109. function TOTAFile.GetAge: TDateTime;
  110. begin
  111.   Result := FAge;
  112. end;
  113.  
  114. function TOTAFile.GetSource: string;
  115. begin
  116.   Result := FSource;
  117. end;
  118.  
  119. { TStringsModuleCreator }
  120.  
  121. constructor TStringsModuleCreator.Create(const FileName: string; Stream: TStringStream;
  122.   Age: TDateTime);
  123. begin
  124.   inherited Create;
  125.   FFileName := FileName;
  126.   FStream := Stream;
  127.   FAge := Age;
  128. end;
  129.  
  130. destructor TStringsModuleCreator.Destroy;
  131. begin
  132.   FStream.Free;
  133.   inherited;
  134. end;
  135.  
  136. procedure TStringsModuleCreator.FormCreated(const FormEditor: IOTAFormEditor);
  137. begin
  138.   { Nothing to do }
  139. end;
  140.  
  141. function TStringsModuleCreator.GetAncestorName: string;
  142. begin
  143.   Result := '';
  144. end;
  145.  
  146. function TStringsModuleCreator.GetCreatorType: string;
  147. begin
  148.   Result := sText;
  149. end;
  150.  
  151. function TStringsModuleCreator.GetExisting: Boolean;
  152. begin
  153.   Result := False;
  154. end;
  155.  
  156. function TStringsModuleCreator.GetFileSystem: string;
  157. begin
  158.   Result := sTStringsFileSystem;
  159. end;
  160.  
  161. function TStringsModuleCreator.GetFormName: string;
  162. begin
  163.   Result := '';
  164. end;
  165.  
  166. function TStringsModuleCreator.GetImplFileName: string;
  167. begin
  168.   Result := FFileName;
  169. end;
  170.  
  171. function TStringsModuleCreator.GetIntfFileName: string;
  172. begin
  173.   Result := '';
  174. end;
  175.  
  176. function TStringsModuleCreator.GetMainForm: Boolean;
  177. begin
  178.   Result := False;
  179. end;
  180.  
  181. function TStringsModuleCreator.GetOwner: IOTAModule;
  182. begin
  183.   Result := nil;
  184. end;
  185.  
  186. function TStringsModuleCreator.GetShowForm: Boolean;
  187. begin
  188.   Result := False;
  189. end;
  190.  
  191. function TStringsModuleCreator.GetShowSource: Boolean;
  192. begin
  193.   Result := True;
  194. end;
  195.  
  196. function TStringsModuleCreator.GetUnnamed: Boolean;
  197. begin
  198.   Result := False;
  199. end;
  200.  
  201. function TStringsModuleCreator.NewFormFile(const FormIdent,
  202.   AncestorIdent: string): IOTAFile;
  203. begin
  204.   Result := nil;
  205. end;
  206.  
  207. function TStringsModuleCreator.NewImplSource(const ModuleIdent, FormIdent,
  208.   AncestorIdent: string): IOTAFile;
  209. begin
  210.   Result := TOTAFile.Create(FStream.DataString, FAge);
  211. end;
  212.  
  213. function TStringsModuleCreator.NewIntfSource(const ModuleIdent, FormIdent,
  214.   AncestorIdent: string): IOTAFile;
  215. begin
  216.   Result := nil;
  217. end;
  218.  
  219. { TStrEditDlg }
  220.  
  221. procedure TStrEditDlg.FileOpen(Sender: TObject);
  222. begin
  223.   with OpenDialog do
  224.     if Execute then Memo.Lines.LoadFromFile(FileName);
  225. end;
  226.  
  227. procedure TStrEditDlg.FileSave(Sender: TObject);
  228. begin
  229.   SaveDialog.FileName := OpenDialog.FileName;
  230.   with SaveDialog do
  231.     if Execute then Memo.Lines.SaveToFile(FileName);
  232. end;
  233.  
  234. procedure TStrEditDlg.UpdateStatus(Sender: TObject);
  235. var
  236.   Count: Integer;
  237.   LineText: string;
  238. begin
  239.   if Sender = Memo then FModified := True;
  240.   Count := Memo.Lines.Count;
  241.   if Count = 1 then LineText := SingleLine
  242.   else LineText := MultipleLines;
  243.   LineCount.Caption := Format('%d %s', [Count, LineText]);
  244. end;
  245.  
  246. { TStringListProperty }
  247.  
  248. function TStringListProperty.GetAttributes: TPropertyAttributes;
  249. begin
  250.   Result := inherited GetAttributes + [paDialog] - [paSubProperties];
  251. end;
  252.  
  253. procedure TStringListProperty.Edit;
  254. var
  255.   Ident: string;
  256.   Component: TComponent;
  257.   Module: IOTAModule;
  258.   Editor: IOTAEditor;
  259.   ModuleServices: IOTAModuleServices;
  260.   Stream: TStringStream;
  261.   Age: TDateTime;
  262. begin
  263.   Component := TComponent(GetComponent(0));
  264.   ModuleServices := BorlandIDEServices as IOTAModuleServices;
  265.   if (TObject(Component) is TComponent) and
  266.     (Component.Owner = Self.Designer.GetRoot) then
  267.   begin
  268.     Ident := Self.Designer.GetRoot.Name + DotSep +
  269.       Component.Name + DotSep + GetName;
  270.     Module := ModuleServices.FindModule(Ident);
  271.   end else Module := nil;
  272.   if (Module <> nil) and (Module.GetModuleFileCount > 0) then
  273.     Module.GetModuleFileEditor(0).Show
  274.   else with TStrEditDlg.Create(Application) do
  275.   try
  276.     Memo.Lines := TStrings(GetOrdValue);
  277.     UpdateStatus(nil);
  278.     FModified := False;
  279.     ActiveControl := Memo;
  280.     CodeEditorItem.Enabled := Ident <> '';
  281.     CodeWndBtn.Enabled := Ident <> '';
  282.     case ShowModal of
  283.       mrOk: SetOrdValue(Longint(Memo.Lines));
  284.       mrYes:
  285.         begin
  286.           Stream := TStringStream.Create('');
  287.           Memo.Lines.SaveToStream(Stream);
  288.           Stream.Position := 0;
  289.           Age := Now;
  290.           Module := ModuleServices.CreateModule(
  291.             TStringsModuleCreator.Create(Ident, Stream, Age));
  292.           if Module <> nil then
  293.           begin
  294.             with StringsFileSystem.GetTStringsProperty(Ident, Component, GetName) do
  295.               DiskAge := DateTimeToFileDate(Age);
  296.             Editor := Module.GetModuleFileEditor(0);
  297.             if FModified then
  298.               Editor.MarkModified;
  299.             Editor.Show;
  300.           end;
  301.         end;
  302.     end;
  303.   finally
  304.     Free;
  305.   end;
  306. end;
  307.  
  308. procedure TStrEditDlg.FormCreate(Sender: TObject);
  309. begin
  310.   HelpContext := hcDStringListEditor;
  311.  
  312.   OpenDialog.HelpContext := hcDStringListLoad;
  313.   SaveDialog.HelpContext := hcDStringListSave;
  314.   SingleLine := srLine;
  315.   MultipleLines := srLines;
  316. end;
  317.  
  318. procedure TStrEditDlg.Memo1KeyDown(Sender: TObject; var Key: Word;
  319.   Shift: TShiftState);
  320. begin
  321.   if Key = VK_ESCAPE then CancelButton.Click;
  322. end;
  323.  
  324. procedure TStrEditDlg.HelpButtonClick(Sender: TObject);
  325. begin
  326.   Application.HelpContext(HelpContext);
  327. end;
  328.  
  329. procedure TStrEditDlg.CodeWndBtnClick(Sender: TObject);
  330. begin
  331.   ModalResult := mrYes;
  332. end;
  333.  
  334. end.
  335.